package com.ruoyi.accountant.service.impl;

import com.ruoyi.accountant.dao.ReasonDetailDao;
import com.ruoyi.accountant.domain.ReasonDetail;
import com.ruoyi.accountant.domain.ReasonDetailExample;
import com.ruoyi.accountant.mapper.ReasonDetailMapper;
import com.ruoyi.accountant.service.ReasonDetailService;
import com.ruoyi.web.web.form.ReasonDetailForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class ReasonDetailServiceImpl implements ReasonDetailService {

    //@Autowired
    private ReasonDetailDao reasonDetailDao;

    @Autowired
    private ReasonDetailMapper reasonDetailMapper;

    @Override
    public void save(ReasonDetail reasonDetail) {
        reasonDetailMapper.insert(reasonDetail);
    }

    @Override
    public List<ReasonDetail> findAll() {
        return reasonDetailMapper.selectByExample(null);
    }

    @Override
    public List<ReasonDetail> findByForm(ReasonDetailForm form) {
        ReasonDetailExample reasonDetailExample = new ReasonDetailExample();
        ReasonDetailExample.Criteria criteria = reasonDetailExample.createCriteria();

        criteria.andTransactionDateGreaterThanOrEqualTo(form.getTransactionStartDate());
        criteria.andTransactionDateLessThanOrEqualTo(form.getTransactionEndDate());

        if(form.getOfReason() != null && form.getOfReason() != -1) {
            criteria.andOfReasonEqualTo(form.getOfReason());
        }
        if((form.getOther() != null && !StringUtils.isEmpty(form.getOther()))) {
            //对象 标签 发票号码 备注
            criteria.andObjectLike("%"+form.getOther()+"%");
            criteria.andTagLike("%"+form.getOther()+"%");
            criteria.andInvoiceLike( "%"+form.getOther()+"%");
            criteria.andCommentLike("%"+form.getOther()+"%");
        }

        return reasonDetailMapper.selectByExample(reasonDetailExample);
    }

    @Override
    public ReasonDetail findById(Long aid) {
        return reasonDetailDao.findById(aid).get();
    }
}
